package com.zanglikun.rocketmq.filter.sql;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

/**
 * @Author: zanglikun
 * @Date: 2022/3/31 20:47
 * @Description: SQL生产者2 只看todo5 内容即可
 */
public class SqlProducer2 {
    public static void main(String[] args) throws Exception {
        // todo 1 创建消息生产者对象,同时指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // todo 2 指定NameServer地址
        producer.setNamesrvAddr("192.168.253.128:9876;192.168.253.129:9876");
        // todo 3 启动生产者
        producer.start();
        for (int i = 1; i <= 10; i++) {
            // todo 4 创建消息对象,同时指定topic、tags、body
            Message message = new Message("FilterTagTopic", "Sql2", ("Sql2 你好"+i).getBytes());
            // todo 5 加入sql标记字段 i ，剩下代码与常规发送代码一致
            message.putUserProperty("i", String.valueOf(i));
            // todo 6 发送消息
            SendResult sendResult = producer.send(message);
            // todo 7 处理发送结果
            SendStatus sendStatus = sendResult.getSendStatus();
            String msgId = sendResult.getMsgId();
            int queueId = sendResult.getMessageQueue().getQueueId();
            System.out.println("消息发送状态" + sendStatus + ",消息Id:" + msgId + ",消息队列Id" + queueId);
            Thread.sleep(100);
        }
        // todo 8 关闭生产者
        producer.shutdown();
    }
}
